package com.atguigu.gulimall.coupon.controller;


import com.atguigu.common.utils.R;
import com.atguigu.gulimall.coupon.entity.CouponEntity;
import com.atguigu.gulimall.coupon.service.CouponService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 优惠券信息 前端控制器
 * </p>
 *
 * @author szh
 * @since 2020-12-01
 */
@RefreshScope
@RestController
@RequestMapping("/coupon/coupon")
public class CouponController {
	@Autowired
	private CouponService couponService;
	@Value("${coupon.user.name}")
	private String name;
	@Value("${coupon.user.age}")
	private Integer age;
	@GetMapping("/member/list")
	public R memberCoupons(){
		CouponEntity coupon = new CouponEntity();
		coupon.setCouponName("满100减50");
		return R.ok().put("coupons", Arrays.asList(coupon));
	}

	@GetMapping("/testConfig")
	public R testConfig(){
		return R.ok().put("name",name).put("age",age);
	}

	@GetMapping("/list")
	public R getList(){
		QueryWrapper<CouponEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("1","1");
		List<CouponEntity> list = couponService.list(queryWrapper);
		if (list.isEmpty()){
			return R.error("查询为空");
		}
		return R.ok().put("code","200").put("data",list);
	}




	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
//    @RequiresPermissions("coupon:coupon:info")
	public R info(@PathVariable("id") Long id){
		CouponEntity coupon = couponService.getById(id);

		return R.ok().put("coupon", coupon);
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
//    @RequiresPermissions("coupon:coupon:save")
	public R save(@RequestBody CouponEntity coupon){
		couponService.save(coupon);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
//    @RequiresPermissions("coupon:coupon:update")
	public R update(@RequestBody CouponEntity coupon){
		couponService.updateById(coupon);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
//    @RequiresPermissions("coupon:coupon:delete")
	public R delete(@RequestBody Long[] ids){
		couponService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}
}
